﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using GestureLib;

namespace GesturePad
{
    public partial class TrainedGesturesConfigurationForm : Form
    {
        public TrainedGesturesConfigurationForm()
        {
            InitializeComponent();
        }

        public GestureLib.GestureLib GestureLib { get; set; }

        private void TrainedGesturesConfigurationForm_Load(object sender, EventArgs e)
        {
            foreach (TrainedGesture trainedGesture in GestureLib.TrainedGestures)
            {
                lvTrainedGestures.Items.Add(CreateListViewItem(trainedGesture));
            }
        }

        private ListViewItem CreateListViewItem(TrainedGesture trainedGesture)
        {
            ListViewItem lvItem = new ListViewItem(trainedGesture.Name);

            lvItem.SubItems.Add(trainedGesture.GestureAlgorithms.Count.ToString());
            lvItem.SubItems.Add(trainedGesture.GestureActions.Count.ToString());
            lvItem.Tag = trainedGesture;

            return lvItem;
        }

        private void lvTrainedGestures_SelectedIndexChanged(object sender, EventArgs e)
        {
            TrainedGesture trainedGesture = GetSelectedTrainedGesture();

            if (trainedGesture != null)
            {
                lvAlgorithms.Items.Clear();
                lvActions.Items.Clear();

                tsbEdit.Enabled = true;
                tsbRemove.Enabled = true;

                lblTrainedGestureName.Text = trainedGesture.Name;

                foreach (IGestureAlgorithm algorithm in trainedGesture.GestureAlgorithms)
                {
                    ListViewItem lvItem = lvAlgorithms.Items.Add(algorithm.Name);
                    lvItem.SubItems.Add(algorithm.GetType().Name);
                }

                foreach (IGestureAction action in trainedGesture.GestureActions)
                {
                    ListViewItem lvItem = lvActions.Items.Add(action.Name);
                    lvItem.SubItems.Add(action.GetType().Name);
                }
            }
            else
            {
                tsbEdit.Enabled = false;
                tsbRemove.Enabled = false;
            }
        }

        private TrainedGesture GetSelectedTrainedGesture()
        {
            TrainedGesture trainedGesture = null;
                 
            if (lvTrainedGestures.SelectedItems.Count > 0)
            {
                trainedGesture = (TrainedGesture)lvTrainedGestures.SelectedItems[0].Tag;
            }

            return trainedGesture;
        }

        private void btnDelete_Click(object sender, EventArgs e)
        {
            TrainedGesture trainedGesture = GetSelectedTrainedGesture();

            if (trainedGesture != null)
            {
                GestureLib.TrainedGestures.Remove(trainedGesture);
            }
        }

        private void btnClose_Click(object sender, EventArgs e)
        {
            Close();
        }

        private void tsbEdit_Click(object sender, EventArgs e)
        {
            TrainedGesture trainedGesture = GetSelectedTrainedGesture();

            if (trainedGesture != null)
            {
                AddEditTrainedGestureForm addEditTrainedGesture = new AddEditTrainedGestureForm();

                addEditTrainedGesture.Text = string.Format("Geste \"{0}\" bearbeiten", trainedGesture.Name);
                addEditTrainedGesture.GestureLib = GestureLib;
                addEditTrainedGesture.TrainedGesture = trainedGesture;
                addEditTrainedGesture.ShowDialog();
            }
        }

        private void tsbAdd_Click(object sender, EventArgs e)
        {
            AddEditTrainedGestureForm addEditTrainedGesture = new AddEditTrainedGestureForm();

            addEditTrainedGesture.Text = "Neue TrainedGesture erstellen";
            addEditTrainedGesture.GestureLib = GestureLib;
            DialogResult dialogResult = addEditTrainedGesture.ShowDialog();

            if (dialogResult == DialogResult.OK)
            {
                GestureLib.TrainedGestures.Add(addEditTrainedGesture.TrainedGesture);
                lvTrainedGestures.Items.Add(
                    CreateListViewItem(addEditTrainedGesture.TrainedGesture));
            }
        }

        private void tsbRemove_Click(object sender, EventArgs e)
        {
            if (lvTrainedGestures.SelectedItems.Count > 0)
            {
                TrainedGesture trainedGesture = GetSelectedTrainedGesture();

                DialogResult dialogResult =
                        MessageBox.Show(
                        string.Format("Soll die Geste \"{0}\" gelöscht werden?", trainedGesture.Name),
                        Text, 
                        MessageBoxButtons.YesNo, 
                        MessageBoxIcon.Question);

                if (dialogResult == DialogResult.Yes)
                {
                    ListViewItem lvItem = lvTrainedGestures.SelectedItems[0];

                    GestureLib.TrainedGestures.Remove(trainedGesture);
                    lvTrainedGestures.Items.Remove(lvItem);
                }
            }
        }
    }
}
